package com.feng.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.feng.entity.Book;
import com.feng.entity.CarItem;
import com.feng.service.ServletProduct;
import com.feng.util.web.ServletUtil;

public class addShopCar extends HttpServlet {


	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		Book b = new ServletProduct().findById(id);
		CarItem item = new CarItem();
		item.setBook(b);
		item.setNum(1);
		HttpSession session = ServletUtil.getSession(request);
		List<CarItem> list = (List<CarItem>) session.getAttribute("shopcar");
		if(list==null){
			list = new ArrayList<CarItem>(); 
			session.setAttribute("shopcar", list);
			 list.add(item);
		}else{
			boolean flag = false;
			for (CarItem c : list) {
				if(id.equals(c.getBook().getId())){
					c.setNum(c.getNum()+1);
					flag = true;
					break;
				}
			}
			if(!flag){
				list.add(item);
			}
		}
		session.setAttribute("shopcar", list);
		ServletUtil.redirect(response, "shopCar.jspx");
	}

}
